package io.cici.cc.mybatis.lite.sql_source.scripting.tag.forEach;

import io.cici.cc.mybatis.lite.sql_source.Context;
import io.cici.cc.mybatis.lite.sql_source.scripting.tag.ForEachTag;

import java.util.Map;

public class SeparatorContext extends Context {
    private final Context delegate;
    private final String prefix;
    private boolean prefixApplied;

    public SeparatorContext(ForEachTag forEachTag, Context delegate, String prefix) {
        super(forEachTag.configuration, null);
        this.delegate = delegate;
        this.prefix = prefix;
        this.prefixApplied = false;
    }

    public boolean isPrefixApplied() {
        return prefixApplied;
    }

    @Override
    public Map<String, Object> getBindings() {
        return delegate.getBindings();
    }

    @Override
    public void bind(String name, Object value) {
        delegate.bind(name, value);
    }

    @Override
    public void appendSql(String sql) {
        if (!prefixApplied && sql != null && sql.trim().length() > 0) {
            delegate.appendSql(prefix);
            prefixApplied = true;
        }
        delegate.appendSql(sql);
    }

    @Override
    public String getSql() {
        return delegate.getSql();
    }

    @Override
    public int getUniqueNumber() {
        return delegate.getUniqueNumber();
    }
}
